<template>
  <div class="settings-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <div class="tips">
            <i class="el-icon-info" />
            <span>总记录数：{{ total }}</span>
          </div>
        </template>
        <template #right>
          <!-- 右侧 -->
          <el-button type="warning" size="small">导入excel</el-button>
          <el-button type="danger" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>
      <!-- table -->
      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop: 'timeOfEntry', order: 'descending'}">
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" fixed="right" width="280">
            <template #default="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployeeFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination layout="prev, pager, next" :total="total" @current-change="pageChange" />
        </div>
      </el-card>
      <!-- 弹出层 -->
      <add-employee :show-dialog="showDialog" @close-dialog="closeDialog" @update-employee="hgetEmployeeList" />
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import AddEmployee from './components/add-employee.vue'
export default {
  name: 'Employee',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        size: 10
      },
      total: 0,
      showDialog: false
    }
  },
  mounted() {
    this.hgetEmployeeList()
  },
  methods: {
    // map聘用形式
    formatEmployment(type) {
      const mapEmployment = {
        1: '正式',
        2: '非正式'
      }
      return mapEmployment[type]
    },
    // 分页获取数据
    pageChange(page) {
      this.params.page = page
      this.hgetEmployeeList()
    },
    // 获取员工列表
    async hgetEmployeeList() {
      const res = await getEmployeeList(this.params)
      const { rows, total } = res.data
      this.list = rows
      this.total = total
    },
    // 删除员工
    delEmployeeFn(id) {
      this.$confirm('你确认要删除么?', '温馨提示').then(async() => {
        await delEmployee(id)
        this.hgetEmployeeList()
        this.$message.success('删除员工成功')
      }).catch(error => {
        console.log(error)
      })
    },
    // 添加员工
    addEmployee() {
      // 显示弹出层
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
 .tips {
    line-height: 34px;
    padding: 0px 15px;
    border-radius: 5px;
    border: 1px solid rgba(145, 213, 255, 1);
    background: rgba(230, 247, 255, 1);
      i {
      margin-right: 10px;
      color: #409eff;
    }
  }
</style>
